//密码输入确认
function passwordConfirm(objID1,objID2){
	if ($('#'+objID1).val() !== $('#'+objID2).val()){
		alert('密码不一致，请重填');
		return false;
	}
	return true;
}

//js时间戳格式化成日期格式
function dateformat(timestamp,chinese){
	if (!timestamp){
		datetime = new Date().toLocaleString();
	}else{
		datetime = new Date(parseInt(timestamp)*1000).toLocaleString();
	}
	if (!chinese){
		return datetime.replace(/年|月/g, '-').replace(/日/g, '');
	}
	return datetime;
}

//打开新窗口
function newWindow(url){
	if (0 > navigator.appName.indexOf('Netscape')){
		if (0 == $('#newWindow').size()){
			$('body').append('<a href="" target="_blank" id="newWindow"></a>');
		}
		document.getElementById('newWindow').href = url;
		document.getElementById('newWindow').click();
	}else{
		window.open(url);
	}
}

//全选反选,默认针对父表格下所有checkbox
function checkall(obj,parents){
    if ((typeof parents != 'string') || parents.length == 0)
        parents = 'table';
	checkboxs = $(obj).parents(parents).eq(0).find('input:checkbox');
	for (i in checkboxs){
		checkboxs.get(i).checked=obj.checked;
	}
}
//表格同行全选
function trcheck(obj){
	checkboxs = $(obj).parents('tr').eq(0).find('input:checkbox');
	for (i in checkboxs){
		checkboxs.get(i).checked=obj.checked;
	}
}
//检查是否已经有选择项
function checkedor(strFormId,strAlert){
	if (0 == $('#'+strFormId).find('input:checkbox:checked[name]:not([name=""])').length){
		alert(strAlert||'尚未选择任何一条记录');
		return false;
	}
	return true;
}

//按下空格时等价于点击行为
pressspace = function(event){
	key = window.event?event.keyCode:event.which;
	obj = event.srcElement||event.target;
	if (32 == key) obj.click();
}

//复制到剪贴板
function copyToClipboard(txt) {
	if (window.clipboardData) {
		window.clipboardData.clearData();
		window.clipboardData.setData("Text", txt);
		//alert("复制成功！")
		return true;
	} else if (navigator.userAgent.indexOf("Opera") != -1) {
		window.location = txt;
	} else if (window.netscape) {
		try {
			netscape.security.PrivilegeManager
					.enablePrivilege("UniversalXPConnect");
		} catch (e) {
			alert("被浏览器拒绝！\n请在浏览器地址栏输入'about:config'并回车\n然后将 'signed.applets.codebase_principal_support'设置为'true'");
			return false;
		}
		var clip = Components.classes['@mozilla.org/widget/clipboard;1']
				.createInstance(Components.interfaces.nsIClipboard);
		if (!clip)
			return false;
		var trans = Components.classes['@mozilla.org/widget/transferable;1']
				.createInstance(Components.interfaces.nsITransferable);
		if (!trans)
			return false;
		trans.addDataFlavor('text/unicode');
		var str = new Object();
		var len = new Object();
		var str = Components.classes["@mozilla.org/supports-string;1"]
				.createInstance(Components.interfaces.nsISupportsString);
		var copytext = txt;
		str.data = copytext;
		trans.setTransferData("text/unicode", str, copytext.length * 2);
		var clipid = Components.interfaces.nsIClipboard;
		if (!clip)
			return false;
		clip.setData(trans, null, clipid.kGlobalClipboard);
		//alert("复制成功！");
		return true;
	}
}

//插入脚本，异步方式
function addscript(scriptUrl){
	scriptUrls = [];
	if ('string' === typeof scriptUrl){
		scriptUrls.push(scriptUrl);
	}else{
		scriptUrls = scriptUrl;
	}
	
	scriptDoms = [];
	if ('number' === typeof scriptUrls.length && 0 < scriptUrls.length){
		for (i in scriptUrls){
			if ('string' !== typeof scriptUrls[i]) continue;
			scriptDom = document.createElement('script');
			scriptDom.type = 'text/javascript';
			scriptDom.src = scriptUrls[i];
			scriptDoms.push(scriptDom);
		}
	}else{
		return false;
	}
	
	if (0 < scriptDoms.length){
		if (0 < (o = document.getElementsByTagName('script')).length){
			o = o[0].parentNode;
		}else if (0 < (o = document.getElementsByTagName('head')).length){
			o = o[0];
		}else if (0 < (o = document.getElementsByTagName('html')).length){
			o = o[0];
		}else{
			return false;
		}
		scriptAdd = 0;
		
		for (i in scriptDoms){
			o.appendChild(scriptDoms[i]);
			scriptAdd++;
		}
		return scriptAdd;
	}else{
		return false;
	}
	return false;
}

//ajax访问
function ajax(url,postData,success_callbak,error_callback,alert){
	var xmlhttp = false;
	if (typeof postData == 'undefined')
		postData = '';
	else
		postData = String(postData);
	try{
		if( window.ActiveXObject ){
			for( var i = 5; i; i-- ){
				try{
					if( i == 2 ){
						xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP" );
					}else{
						xmlhttp = new ActiveXObject( "Msxml2.XMLHTTP." + i + ".0" );
					}
					break;
				}catch(e){
					xmlhttp = false;
				}
			}
		}else if( window.XMLHttpRequest ){
			xmlhttp = new XMLHttpRequest();
		}
	}catch(e){
		alert('Initialization error: '+String(e)+' in Line '+e.lineNumber);
		return false;
	}
	if (xmlhttp){
		try{
			xmlhttp.open('POST',url,true);
			xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
			xmlhttp.setRequestHeader("Charset","utf-8");
			xmlhttp.onreadystatechange = function(){
				if (xmlhttp.readyState != 4)
					return;
				// 收到完整的服务器响应
				if (xmlhttp.status != 200){//执行失败
					if (typeof error_callback == 'function')
						error_callback(false);
					if (alert == true)
						alert ('ajax failed: '+xmlhttp.responseText);
				}
				{//执行成功
					if (typeof success_callbak == 'function')
						success_callbak(xmlhttp.responseText);
					if (alert == true)
						alert(xmlhttp.responseText);
				}
			}
			xmlhttp.send(postData);
		}catch(e){
			alert('Execution error: '+String(e)+' in Line '+e.lineNumber);
			return false;
		}
		return true;
	}
	return false;
}


function rand(num){
	return parseInt(Math.random()*num+1);
}

//将json字符串转换成js对象
function json_decode(str){
	if (!arguments.length || String(str).length == 0)
		return [];
	return eval('('+str+')');
}

function sleep(second,callback){
	timestamp = Number(new Date())+second*1000;
	while (new Date() < timestamp) {}
	if (typeof callback == 'function')
		callback();
	return true;
}

function beforeunload(str){
	if (arguments.length > 0 && (str = String(str)).length > 0)
		return Boolean(window.onbeforeunload = function(){
			if (/Firefox[\/\s](\d+)/.test(navigator.userAgent) && new Number(RegExp.$1) >= 4) {
			      if(!confirm(str))
			    	  window.setTimeout(function(){window.stop();},1);
			  } else
				  return str;
		});
	return Boolean(window.onbeforeunload = function(){return true;});
}
function unsetunload(){
	window.onbeforeunload = null;
	window.onunload = null;
	return true;
}